package club.kingyin.kyblog.core.business.mapper;

import club.kingyin.kyblog.core.business.dao.Doc;
import club.kingyin.kyblog.core.business.dao.Query;
import club.kingyin.kyblog.core.business.vo.admin.TypeCount;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.LinkedHashSet;

/**
 * @author : kingyins
 * @version : 1.0
 * @program : ky-blog
 * @description : 在此处添加类描述
 * @create : 2021年08月24日 10:07
 **/
@Mapper
public interface DocMapper {

    int insert(Doc doc);

    int updateById(Doc doc);

    @Update("update docs set author = #{author}")
    int updateAuthor(String author);

    @Select("select count(*) value, type name from docs group by type")
    LinkedHashSet<TypeCount> typeCount();

    @Select("select count(*) from docs")
    int docCount();

    @Select("select ifnull(sum(disComment),0) from docs")
    int disCommentCount();

    @Select("select ifnull(sum(comment),0) from docs where comment > 0")
    int commentCount();

    @Select("select ifnull(sum(viewCount),0) from docs")
    int viewCount();

    @Select("select id,viewCount from docs order by viewCount desc limit 0,10")
    LinkedHashSet<Doc> hotDocLimit();


    @Update("update docs set viewCount = viewCount + #{viewCount} where id = #{id}")
    int addViewCountById(int id, int viewCount);

    @Select("select * from docs where id = #{id}")
    Doc selectById(int id);

    @Select("select comment from docs where id = #{id}")
    int selectCommentById(int id);

    @Delete("delete from docs where id = #{id}")
    int deleteById(int id);

    @Select("select count(*) from (" +
            "select id,title,img,importance,displayTime,viewCount,summary,comment from docs where status = 'published' and type = #{context} " +
            "union " +
            "select id,title,img,importance,displayTime,viewCount,summary,comment from docs where status = 'published' and title in (select title from docs where title like CONCAT('%', #{context ,jdbcType=VARCHAR}, '%')) " +
            "union " +
            "select id,title,img,importance,displayTime,viewCount,summary,comment from docs where status = 'published' and summary in (select summary from docs where summary like CONCAT('%', #{context ,jdbcType=VARCHAR}, '%')) " +
            ") as a;")
    int selectCount(String context);

    @Select("select count(*) from docs where status = 'published' and type = #{type}")
    int selectCountByType(String type);

    @Select("select count(*) from docs where status = 'published'")
    int selectPublishedCount();

    @Select("select * from (select id,title,img,importance,displayTime,viewCount,summary,comment from docs where status = 'published' and type = #{context} " +
            "union all " +
            "select id,title,img,importance,displayTime,viewCount,summary,comment from docs where status = 'published' and title in (select title from docs where title like CONCAT('%', #{context ,jdbcType=VARCHAR}, '%')) " +
            "union all " +
            "select id,title,img,importance,displayTime,viewCount,summary,comment from docs where status = 'published' and summary in (select summary from docs where summary like CONCAT('%', #{context ,jdbcType=VARCHAR}, '%'))) as a order by a.importance desc, a.displayTime desc limit #{page},#{size}")
    LinkedHashSet<Doc> selectDocsByTitleOrSummaryOrType(String context, int page, int size);

    @Select("select id,title,img,importance,displayTime,viewCount,summary,comment from docs where status = 'published' and type = #{type} order by importance desc, displayTime desc limit #{page},#{size}")
    LinkedHashSet<Doc> selectDocsByType(String type, int page, int size);

    @Select("select id,title,img,importance,displayTime,viewCount,summary,comment from docs where status = 'published' order by importance desc, displayTime desc limit #{page},#{size}")
    LinkedHashSet<Doc> selectPublishedDocs(int page, int size);

    LinkedHashSet<Doc> selectDocsAsAdmin(Query query);

    int selectCountByAdmin(Query query);

}
